<?php
class BMSC_Member extends BMSC_Db
{
	public function checkLogin($email, $password)
	{
		try
		{
			$query = sprintf("CALL sp_admin_check_login('%s', '%s')", $email, md5($password));
			$member = $this->_readerDb->fetchRow($query);
			$this->_readerDb->closeConnection();
			return $member;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function changePassword($email, $currentPassword, $newPassword){
		try {
			$query = "CALL sp_member_update_password(?, ?, ?)";
			$stmt = $this->_writerDb->query($query, array($email, md5($currentPassword), md5($newPassword)));
			return $result = $stmt->execute();
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	public function checkAdmin($id)
	{
		try {				
			$query = "CALL sp_member_check_admin(?)";
			$stmt = $this->_readerDb->query($query, array($id));
			$r = $stmt->fetchAll();		
			$stmt->closeCursor();
			return $r;
		} catch (Exception $e) {
			throw $e;
		}
	}


	 
	public function getMemberList($from, $numberOfMembers){		
		try {
			$query = "CALL sp_admin_get_member_list(?, ?)";
			$stmt = $this->_readerDb->query($query, array($from, $numberOfMembers));
			return $stmt->fetchAll();			
		} catch (Exception $e) {
			throw $e;
		}
	 }
	 
	 public function getMyself($id)
	 {
		try {
			$query = "CALL sp_member_get_member(?)";
			$stmt = $this->_readerDb->query($query, $id);
			return $stmt->fetchAll();			
		} catch (Exception $e) {
			throw $e;
		}
	 }

	public function getMember($memberID)
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_member(%u)", $memberID);
			$member = $this->_readerDb->fetchRow($query);
			$this->_readerDb->closeConnection();
			return $member;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}

		return $member;
	}
	
	public function addMember($fullName, $email, $password, $isAdmin, $isActive)
	{	
		try
		{
			$memberInfo = array();
			$memberInfo["FULLNAME"] = $fullName;
			$memberInfo["EMAIL"] = $email;
			$memberInfo["PASSWORD"] = md5($password);
			$memberInfo["IS_ADMIN"] = $isAdmin;
			$memberInfo["IS_ACTIVE"] = $isActive;		
			$memberInfo["IS_DELETE"] = 0;
			return $this->_writerDb->insert("member", $memberInfo);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function editMember($memberID, $fullName, $email, $password, $isAdmin, $isActive, $changePassword)
	{
		try
		{
			$memberInfo = array();
			$memberInfo["FULLNAME"] = $fullName;
			$memberInfo["EMAIL"] = $email;
			if($changePassword) {
				$memberInfo["PASSWORD"] = md5($password);
			}
			$memberInfo["IS_ADMIN"] = $isAdmin;
			$memberInfo["IS_ACTIVE"] = $isActive;
			return $this->_writerDb->update("member", $memberInfo, "ID=" . $memberID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function editMyself($memberID, $fullName, $email, $password, $changePassword)
	{
		try
		{
			$memberInfo = array();
			$memberInfo["FULLNAME"] = $fullName;
			$memberInfo["EMAIL"] = $email;
			if($changePassword) {
				$memberInfo["PASSWORD"] = md5($password);			
			}
			return $this->_writerDb->update("member", $memberInfo, "ID=" . $memberID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}		
	}
	
	public function removeMember($memberID)
	{
		try
		{
			$memberInfo = array();
			$memberInfo["IS_DELETE"] = 1;
			return $this->_writerDb->update("member", $memberInfo, "ID=" . $memberID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	


	public function activateMember($activateCode)
	{
		$dbWriter = ZDb::connect(DB_DNS_WRITER);
		$query = sprintf("call sp_member_activate('%s')", $activateCode);
		$result = $dbWriter->extended->getOne($query);
		$dbWriter->disconnect();

		return $result;
	}

	public function resetPassword($memberEmail, $memberLoginPasswordNew, $updatedBy){
		$this->_dbWriter->connect();
		$query = sprintf("call sp_member_reset_password('%s', '%s', '%s')", $memberEmail, $memberLoginPasswordNew, $updatedBy);
		$result = $this->_dbWriter->extended->getRow($query);
		$this->_dbWriter->disconnect();
		return $result['varError'];
	 }



	 /*
	 *	updateMember: cập nhật thông tin member
	 *	input: memberInfo : memberID, p_v_memberFirstName varchar(30), p_v_memberLastName varchar(30), p_v_memberEmail varchar(100),
	 *						p_v_memberLoginPassword varchar(32), p_i_genderID tinyint, p_v_memberBirthDay date,
	 *						p_v_memberAddress varchar(100), p_i_districtID int, p_i_cityID int, p_i_countryID int,
	 *						p_v_memberAddressDescription varchar(300), p_v_memberPhone varchar(20),
	 *						p_v_memberMobile varchar(20)
	 *	output: error
	 *		+ 0 : thành công
	 *		+ -1: lỗi hệ thống
	 *		+ -2: memberID không tồn tại
	 */
	public function updateMember($memberInfo){
		$this->_dbWriter->connect();
		$query = sprintf("call sp_member_register(%u, '%s', '%s', %u, '%s', '%s', %u, %u, %u, '%s', '%s', '%s')",  $memberInfo['memberID'], $memberInfo['memberFirstName'], $memberInfo['memberLastName'], $memberInfo['genderID'], $memberInfo['memberBirthDay'], $memberInfo['memberAddress'], $memberInfo['districtID'], $memberInfo['cityID'], $memberInfo['countryID'], $memberInfo['memberAddressDescription'], $memberInfo['memberPhone'], $memberInfo['memberMobile']) ;
		$result = $this->_dbWriter->extended->getRow($query);
		$this->_dbWriter->disconnect();
		return $result['varError'];
	 }



	 /*
	 *	getCountryList: hàm lấy danh sách quốc gia
	 *	input: không có
	 *	output: danh sách quốc gia
	 *		+ countryID
	 *		+ countryName
	 */
	public function getCountryList(){
		$this->_dbReader->connect();

		$query = sprintf("call sp_country_get_list()");
		$result = $this->_dbReader->extended->getAll($query);
		$this->_dbReader->disconnect();
		return $result;
	 }

	/*
	 *	getCityList: hàm lấy danh sách thành phố
	 *	input: countryID
	 *	output: danh sách thành phố
	 *		+ cityID
	 *		+ cityName
	 */
	public function getCityList($countryID){
		$this->_dbReader->connect();
		$query 		= sprintf("call sp_city_get_list(%u)", $countryID);
		$result = $this->_dbReader->extended->getAll($query);
		$this->_dbReader->disconnect();
		return $result;
	 }

	/*
	 *	getDistrictList: hàm lấy danh sách quận huyện
	 *	input: cityID
	 *	output: danh sách quận huyện
	 *		+ districtID
	 *		+ districtName
	 */
	public function getDistrictList($cityID){
	 	$this->_dbReader->connect();
		$query 		= sprintf("call sp_district_get_list(%u)", $cityID);
		$result = $this->_dbReader->extended->getAll($query);
		$this->_dbReader->disconnect();
		return $result;
	 }

	 public function check_session() {
			// check Session
			$sessionNamespace = new Zend_Session_Namespace('ZoKIK');
			if ($sessionNamespace->username != "" && $sessionNamespace->userID != "" && $sessionNamespace->lastname != "")
				return true;
			else {
				return false;
			}
		}

	 /*
	 *	cityList: hàm danh sách quốc gia của selectbox
	 *	input: $countryID
	 *	output: string
	 */
	public function countryList($countryID){
	 	$countryList = $this->getCountryList();
		$strTemp = '';
		foreach($countryList as $key=>$val) {
			if($countryID == $val['countryID'])
				$strTemp .= "<option  selected='selected' value=".$val['countryID'].">".$val['countryName']."</option>";
			else
				$strTemp .= "<option  value=".$val['countryID'].">".$val['countryName']."</option>";
		}
		return $strTemp;
	 }

	 /*
	 *	cityList: hàm danh sách thành phố của selectbox
	 *	input: $countryID, $cityID
	 *	output: string
	 */
	public function cityList($countryID, $cityID){
	 	$cityList = $this->getCityList($countryID);
		$strTemp = '';
		foreach($cityList as $key=>$val) {
			if($cityID == $val['cityID'])
				$strTemp .= "<option  selected='selected' value=".$val['cityID'].">".$val['cityName']."</option>";
			else
				$strTemp .= "<option  value=".$val['cityID'].">".$val['cityName']."</option>";
		}
		return $strTemp;
	 }

	  /*
	 *	districtList: hàm danh sách quận/huyện của selectbox
	 *	input: $cityID, $districtID
	 *	output: string
	 */
	 public function districtList($cityID, $districtID){
	 	$districtList = $this->getDistrictList($cityID);
		$strTemp = '';
		foreach($districtList as $key=>$val) {
			if($districtID == $val['districtID'])
				$strTemp .= "<option  selected='selected' value=".$val['districtID'].">".$val['districtName']."</option>";
			else
				$strTemp .= "<option  value=".$val['districtID'].">".$val['districtName']."</option>";
		}
		return $strTemp;
	 }

	 /*
	 *	resetPassword: reset lại password
	 *	input:
	 *		+ memberID
	 *		+ encryptedData
	 *	output: error
	 *		+ 0 : thành công
	 *		+ -1: lỗi hệ thống
	 *		+ -2: memberID không tồn tại hay đã active rồi
	 */
	public function activeMember($memberID, $encryptedData){
		$this->_dbWriter->connect();
		$query = sprintf("call sp_member_active(%u, '%s')", $memberID, $encryptedData);
		$result = $this->_dbWriter->extended->getRow($query);
		$this->_dbWriter->disconnect();
		return $result['varError'];
	 }
}
?>